// vuex的权限模块
import { constantRoutes } from '@/router'
import { asyncRouters } from "@/router/index"
// vuex中的permission模块用来存放当前的 静态路由 + 当前用户的 权限路由

const state = {
    routes: constantRoutes
}
const mutations = {
    setRoutes(state, newRoutes) {
        //传入静态路由合并动态路由
        state.routes = [...constantRoutes, ...newRoutes]
    }
}
const actions = {
    filtersRoutes(context, menus) {
        const routes = []
            //过滤出有给予权限的可访问的路由
        menus.forEach(key => {
            routes.push(...asyncRouters.filter(item => key === item.name))
        })
        context.commit('setRoutes', routes)
        return routes
    }
}



export default {
    namespaced: true,
    mutations,
    state,
    actions,
}